
<template>
  <div>
    <Card ref="card">
      <p slot="title">核查报告-{{ groupname }}</p>
      <Row :gutter="16">
        <Col span="3">
          <Select
            v-model="town"
            size="large"
            @on-change="
              (s) => {
                Search();
              }
            "
            placeholder="选择乡镇"
          >
            <Option
              v-for="item in townList"
              :value="item.TownName"
              :key="item.TownName"
              >{{ item.TownName }}</Option
            >
          </Select>
        </Col>

        <Col span="5">
          <Input
            v-model="pName"
            size="large"
            placeholder="姓名/身份证号/住址"
          />
        </Col>

        <Col span="8">
          <Button @click="Search" size="large" type="primary" icon="ios-search"
            >查询</Button
          >
        </Col>

        <Col span="4">
          <Button
            @click="Export"
            size="large"
            type="primary"
            :loading="exportLoading"
            icon="md-download"
            >导出</Button
          >
        </Col>
      </Row>
    </Card>

    <Row class="margin-top-10">
      <Table
        border
        :columns="tableTitle"
        :data="tableData"
        ref="table"
        :height="tableHeight"
      ></Table>
    </Row>
    <Row class="margin-top-10">
      <Page
        :total="total"
        :current="pPage"
        :page-size="pSize"
        :page-size-opts="[50, 100, 300, 500, 1000, 2000, 5000]"
        @on-change="
          (s) => {
            pPage = s;
            Search();
          }
        "
        @on-page-size-change="
          (p) => {
            pSize = p;
            pPage = 1;
            Search();
          }
        "
        show-total
        show-sizer
      />
    </Row>
    <Modal :mask-closable="false" v-model="modal2" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>请输入打印标题</span>
      </p>
      <Row>
        <Input v-model="pringtTitle" size="large" placeholder="打印标题" />
      </Row>
      <br />

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="Print">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import excel from "@/libs/excel";
import { printUrl } from "@/libs/util";
import { Message, Modal } from "view-design";
import { GetGroupTown, SearchEmployee, SaveActionLog } from "@/api/data";
import { SearchSavedDBPerson } from "@/api/dbdata";
export default {
  name: "db_search_savedperson",
  data() {
    return {
      printSrc1: printUrl + "printlist.html",
      printSrc2: printUrl + "printlistreport.html",
      printSrc3: printUrl + "printlistreport2.html",
      printSrc4: printUrl + "printlistreport3.html",

      total: 0,
      pPage: 1,
      pSize: 50,
      tableHeight: 450,
      modal2: false,
      personTemp: {},
      exportLoading: false,
      tableTitle: [
        {
          title: "序号",
          key: "R_INDEX",
          align: "center",
          width: 80,
        },
        {
          title: "姓名",
          key: "Name",
          align: "center",
          width: 120,
        },
        {
          title: "性别",
          key: "Sex",
          align: "center",
          width: 100,
        },
        {
          title: "住址",
          key: "Address",
          align: "center",
          minWidth: 150,
        },
        {
          title: "身份证号",
          key: "IdCardNo",
          minWidth: 150,
          align: "center",
        },
        {
          title: "状态",
          align: "center",
          width: 120,
          render: (h, rparams) => {
            if (rparams.row.DBInfo == null || rparams.row.DBInfo.Gid == null) {
              return h(
                "Tag",
                {
                  props: {
                    color: "default",
                  },
                },
                "待核查"
              );
            } else {
              return h(
                "Button",
                {
                  props: {
                    type: this.GetDBState(1, rparams.row.DBInfo.State),
                    size: "small",
                  },
                  on: {
                    click: () => {
                      if (rparams.row.IsSaved == 1) {
                        this.$router.push({
                          name: "db_info_saved",
                          params: {
                            id: rparams.row.Gid,
                            name: rparams.row.Name,
                          },
                        });
                      } else {
                        this.$router.push({
                          name: "db_info",
                          params: {
                            id: rparams.row.Gid,
                            name: rparams.row.Name,
                          },
                        });
                      }
                    },
                  },
                },
                rparams.row.StateName
              );
            }
          },
        },
        {
          title: "备注",
          align: "center",
          key: "Remark",
          render: (h, params) => {
            if (params.row.DBInfo == null) {
              return h();
            } else {
              return h("div", params.row.DBInfo.Remark);
            }
          },
        },
      ],
      groupname: "",
      pringtTitle: "",
      tableData: [],
      pName: "",
      town: "",
      townList: [],
      tempEmp: "0",
      empList: [],
    };
  },
  props: {
    id: {
      type: String,
      required: true,
    },
  },
  watch: {},
  methods: {
    GetGroupTown() {
      let vm = this;
      GetGroupTown(vm.id).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.townList = data.Data;
          var all = { TownName: "所有乡镇" };
          vm.townList.unshift(all);
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    SearchEmployee() {
      let vm = this;
      SearchEmployee({ type: 1 }).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.empList = data.Data;
          var all = { EmployeeName: "所有员工", Gid: "0" };
          vm.empList.unshift(all);
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    GetDBState(type, state) {
      if (type == 1) {
        if (state == 1) {
          return "success";
        } else if (state == 2) {
          return "error";
        } else if (state == 3) {
          return "warning";
        } else {
          return "";
        }
      } else {
        if (state == 1) {
          return "符合";
        } else if (state == 2) {
          return "不符合";
        } else if (state == 3) {
          return "待复议";
        } else {
          return "未知";
        }
      }
    },
    Search() {
      let vm = this;
      var reqData = {
        page: vm.pPage,
        size: vm.pSize,
        groupid: vm.id,
        grouptown: vm.town == "所有乡镇" ? "" : vm.town,
        state: 1,
        str: vm.pName,
        empid: vm.tempEmp == "0" ? "" : vm.tempEmp,
        assesstype: vm.AssessType,
      };
      SearchSavedDBPerson(reqData).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.total = data.Data.Total;
          vm.tableData = data.Data.Rows;
          if (vm.AssessType == 0) {
            document
              .getElementById("iframe-print-report")
              .contentWindow.LoadData(vm.tableData);
          } else if (vm.AssessType == 1) {
            document
              .getElementById("iframe-print-report2")
              .contentWindow.LoadData(vm.tableData);
          } else if (vm.AssessType == 3) {
            document
              .getElementById("iframe-print-report3")
              .contentWindow.LoadData(vm.tableData);
          }
        } else {
          Message.error(data.Message);
        }
      });
    },
    Export() {
      if (this.tableData.length) {
        const sorted = this.groupBy(this.tableData, function (item) {
          return [item.GroupTown];
        });

        this.exportLoading = true;
        // const params = {
        //   title: [
        //     "序号",
        //     "姓名",
        //     "性别",
        //     "住址",
        //     "身份证号",
        //     "评估结果",
        //     "是否新增",
        //     "备注",
        //   ],
        //   key: [
        //     "R_INDEX",
        //     "Name",
        //     "Sex",
        //     "Address",
        //     "IdCardNo",
        //     "StateName",
        //     "IsNew",
        //     "Remark",
        //   ],
        //   data: sorted,
        //   autoWidth: true,
        //   filename: this.groupname,
        // };
        const params = {
          title: [
            "序号",
            "姓名",
            "性别",
            "乡镇",
            "村居/社区",
            "住址",
            "身份证号",
             "联系方式",
            "是否建档立卡",
            "状态",
            "保障人口",
            "符合人员",
            "家庭年人均收入",
            "是否新增",
            "备注",
            "情况描述",
            "核查备注",
            "核查人员",
          ],
          key: [
            "R_INDEX",
            "Name",
            "Sex",
            "Town",
            "Community",
            "Address",
            "IdCardNo",
            "Phone",
            "IsRecordName",
            "StateName",
            "EnsureCount",
            "FinalResult2",
            "InComeAvg",
            "IsNew",
            "Remark",
            "PoorDesc",
            "DBRemark",
            "EmployeeName",
          ],
          data: sorted,
          autoWidth: true,
          filename: this.groupname,
        };
        excel.export_array_to_excel_more(params);
        this.exportLoading = false;
        this.SaveActionLog("导出", this.groupname);
      } else {
        this.$Message.info("表格数据不能为空！");
      }
    },
    groupBy(array, f) {
      const groups = {};
      array.forEach(function (o) {
        const group = f(o);
        groups[group] = groups[group] || [];
        groups[group].push(o);
      });
      return Object.keys(groups).map(function (group) {
        return groups[group];
      });
    },
    InputPrintTitle() {
      if (!this.pringtTitle) {
        this.pringtTitle = this.groupname;
      }
      this.modal2 = true;
    },
    Print() {
      this.modal2 = false;
      var tableobj = {
        Table: this.tableData,
      };
      document
        .getElementById("iframe-print")
        .contentWindow.printData(this.pringtTitle, JSON.stringify(tableobj));
      this.SaveActionLog("打印", this.groupname);
    },
  
    SaveActionLog(vaction, vfield) {
      var reqData = {
        Action: vaction,
        Filed: vfield,
      };
      SaveActionLog(reqData).then((res) => {});
    },
  },
  created() {},
  mounted() {
    var cardHeight = this.$refs.card.$el.offsetHeight;
    this.tableHeight = window.innerHeight - cardHeight - 200;
    this.groupname = this.$route.query.groupname;
    this.GetGroupTown();
    this.SearchEmployee();
    this.Search();
  },
};
</script>
